import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Index } from "typeorm";
import { ManualTimestamp } from "../decorators/manual-timestamp.decorator";
import { CharacterEntity } from "./character.entity";

@Entity("private_messages")
@ManualTimestamp()
@Index(["senderCharacterId", "receiverCharacterId"])
@Index(["receiverCharacterId", "created_at"])
@Index(["senderCharacterId", "created_at"])
export class PrivateMessageEntity {
  @PrimaryGeneratedColumn({
    comment: "私信消息ID" })
  id!: number;

  @Column("text", { comment: "消息内容" })
  content!: string;

  @Column("varchar", { length: 64, comment: "发送者名称" })
  senderName!: string;

  @Column("int", { comment: "发送者角色ID" })
  senderCharacterId!: number;

  @Column("varchar", { length: 64, comment: "接收者名称" })
  receiverName!: string;

  @Column("int", { comment: "接收者角色ID" })
  receiverCharacterId!: number;

  @Column("varchar", {
    length: 50,
    comment: "消息时间显示格式 (如: 2025-09-02 08:29:08)" })
  messageTime!: string;

  @Column("tinyint", { default: 1, comment: "是否有效 (1:有效, 0:已删除)" })
  isActive!: number;

  @Column("tinyint", { default: 0, comment: "是否已读 (0:未读, 1:已读)" })
  isRead!: number;

  @Column("tinyint", {
    default: 0,
    comment: "消息状态 (0:正常, 1:已撤回, 2:被删除)" })
  status!: number;

  @Column("tinyint", {
    default: 1,
    comment: "消息类型 (1:玩家消息, 2:系统消息)" })
  messageType!: number;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;
@Column("datetime", { name: "updated_at", nullable: true, comment: "更新时间" })
  updated_at!: Date | null;
// 关联关系
  @ManyToOne(() => CharacterEntity, { nullable: true })
  @JoinColumn({ name: "senderCharacterId" })
  senderCharacter!: CharacterEntity;

  @ManyToOne(() => CharacterEntity, { nullable: true })
  @JoinColumn({ name: "receiverCharacterId" })
  receiverCharacter!: CharacterEntity;
}
